const { _ } = window;

class LoginUser {
  constructor(userWithRoles) {
    this.id = userWithRoles.id;
    this.email = userWithRoles.email;
    this.nick = userWithRoles.nick;
    this.avatar = userWithRoles.avatar;
    this.status = userWithRoles.status;
    this.mobile = userWithRoles.mobile;
    this.roles = userWithRoles.roles;
  }

  isActive() {
    return this.status === 'ACTIVE';
  }

  hasRole(roleType, targetId) {
    if (_.isEmpty(this.roles)) {
      return false;
    }
    const targetRole = _.find(this.roles, role => {
      return role.role === roleType && (targetId === undefined || role.targetId === targetId);
    });
    return targetRole !== undefined;
  }
}

export default LoginUser;
